package gouhuo.allbugs.top.controller;

import gouhuo.allbugs.top.common.BaseResponse;
import gouhuo.allbugs.top.common.BusinessException;
import gouhuo.allbugs.top.common.ErrorCode;
import gouhuo.allbugs.top.model.request.chat.ChatListDelRequest;
import gouhuo.allbugs.top.model.request.chat.ChatStatusDelRequest;
import gouhuo.allbugs.top.model.vo.ChatVO;
import gouhuo.allbugs.top.service.ChatService;
import gouhuo.allbugs.top.utils.ChatKeyUtil;
import gouhuo.allbugs.top.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

import static gouhuo.allbugs.top.constant.RedisConstants.CHAT_MSG_KET;

/**
 * 消息
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private ChatService chatService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("获取私聊信息")
    @GetMapping("/privateChat")
    public BaseResponse<List<ChatVO>> priChat(@RequestParam("toId") Long toId, @RequestParam("fromId") Long fromId) {
        if (toId == null || fromId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(chatService.priChat(toId, fromId));
    }

    @ApiOperation("获取队伍聊天信息")
    @GetMapping("/teamChat")
    public BaseResponse<List<ChatVO>> teamChat(@RequestParam("teamId") Long teamId, @RequestParam("userId") Long userId) {
        if (teamId == null || userId == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(chatService.teamChat(teamId, userId));
    }

    @ApiOperation("获取消息提醒信息")
    @GetMapping("/chatStatus")
    public BaseResponse<String> chatStatus(@RequestParam("userId") Long userId) {
        if (userId == null || userId == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(chatService.chatStatus(userId));
    }

    @ApiOperation("获取消息列表")
    @GetMapping("/chatList")
    public BaseResponse<List<ChatVO>> chatList(@RequestParam("userId") Long userId, @RequestParam("scope") Integer scope) {
        if (userId == null || scope == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(chatService.chatList(userId, scope));
    }

    @ApiOperation("删除对应的消息提醒")
    @PostMapping("/statusDel")
    public BaseResponse<Long> statusDel(@RequestBody ChatStatusDelRequest chatStatusDelRequest) {
        if (chatStatusDelRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<String> sysStatus = chatStatusDelRequest.getSysStatus();
        String statusKey = CHAT_MSG_KET + chatStatusDelRequest.getToId() + ":status";
        return ResultUtil.success(stringRedisTemplate.opsForSet().remove(statusKey, sysStatus.toArray()));
    }

    @ApiOperation("删除对应的消息列表项")
    @PostMapping("/chatListDel")
    public BaseResponse<Long> chatListDel(@RequestBody ChatListDelRequest chatListDelRequest) {
        if (chatListDelRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer scope = chatListDelRequest.getScope();
        Long toId = chatListDelRequest.getToId();
        if (scope == null || toId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long del;
        if (scope == 1) {
            Long fromId = chatListDelRequest.getFromId();
            del = chatService.chatListDel(scope, toId, fromId, null);
        } else if (scope == 2) {
            Long teamId = chatListDelRequest.getTeamId();
            del = chatService.chatListDel(scope, toId, teamId, null);
        } else {
            List<String> sysStatus = chatListDelRequest.getSysStatus();
            del = chatService.chatListDel(scope, toId, null, sysStatus);
        }
        if (del == 0) throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        return ResultUtil.success(del);
    }

    @ApiOperation("是否已经打过招呼了")
    @GetMapping("/sayHello")
    public BaseResponse<Boolean> sayHello(@NotNull @RequestParam("toId") Long toId, @NotNull @RequestParam("fromId") Long fromId) {
        String key = ChatKeyUtil.getPriKey(toId, fromId);
        Long size = stringRedisTemplate.opsForList().size(key);
        if (size == null || size <= 0) return ResultUtil.success(false);
        return ResultUtil.success(true);
    }

}
